package com.orionlily.redis3t.zkcurator;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author li.lc
 */
@Configuration
public class CuratorConfig {

    @Bean(name = "curatorFramework")
    public CuratorFramework curatorFramework() {
        /*
         * 重试的间隔时间,毫秒
         */
        int elapsedTimeMs = 5000;
        /*
         * 连接重试次数
         */
        int retryCount = 3;
        /*
         * 连接超时时间，毫秒
         */
        int connectionTimeoutMs = 5000;
        /*
         * session超时时间，毫秒
         */
        int sessionTimeoutMs = 60000;
        /*
         * zookeeper连接地址
         */
        String connectString = "192.168.6.59:2181";
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(elapsedTimeMs, retryCount);
        CuratorFramework curatorFramework = CuratorFrameworkFactory.builder()
                .connectString(connectString)
                .sessionTimeoutMs(sessionTimeoutMs)
                .connectionTimeoutMs(connectionTimeoutMs)
                .retryPolicy(retryPolicy)
                .build();
        curatorFramework.start();
        return curatorFramework;
    }
}
